<template>
  <div class="app-container">
    <el-row :gutter="10">
      <!--树结构-->
      <el-col :span="4" :xs="24">
        <div class="app-container-tree-border">
          <el-tree :data="syTypeList" :props="defaultProps" :expand-on-click-node="false" ref="tree" node-key="id"
            highlight-current @node-click="handleNodeClick">
            <div slot-scope="{node, data}" class="custom-tree-node" :title="node.label" v-text="node.label"></div>
          </el-tree>
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="0">
          <el-form-item label="" prop="baogrq">
            <el-date-picker clearable v-model="queryParams.baogrq" type="date" value-format="yyyy-MM-dd"
              placeholder="请选择报告日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="" prop="jiancdw">
            <el-input v-model="queryParams.jiancdw" placeholder="请输入检测单位" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="" prop="jiancnr">
            <el-input v-model="queryParams.jiancnr" placeholder="请输入检测内容" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="" prop="yangpmc">
            <el-input v-model="queryParams.yangpmc" placeholder="请输入样品名称" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb5 top-right-btn small-line-height">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
              v-hasPermi="['ww:bsWwsy:add']">新增</el-button>
          </el-col>
          <!-- <el-col :span="1.5">
            <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
              v-hasPermi="['ww:bsWwsy:edit']">修改</el-button>
          </el-col> -->
          <!-- <el-col :span="1.5">
            <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
              v-hasPermi="['ww:bsWwsy:export']">导出</el-button>
          </el-col> -->
        </el-row>

        <el-table border stripe v-loading="loading" :data="bsWwsyList" @selection-change="handleSelectionChange">
          <!-- <el-table-column type="selection" width="55" align="center" /> -->
          <el-table-column label="报告编号" align="center" prop="baogbh" min-width="120" />
          <el-table-column label="报告日期" align="center" prop="baogrq" width="120">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.baogrq, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="检测单位" align="center" prop="jiancdw" min-width="120" />
          <el-table-column label="检测内容" align="center" prop="jiancnr" min-width="120" />
          <el-table-column label="材料类型" align="center" prop="jctzVo.clType" min-width="120">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.cl_type" :value="scope.row.jctzVo.clType" />
            </template>
          </el-table-column>
          <el-table-column label="规格型号" align="center" prop="jctzVo.guigxh" min-width="120" />
          <el-table-column label="进场数量" align="center" prop="jctzVo.jincsl" min-width="120" />
          <el-table-column label="生产厂家" align="center" prop="jctzVo.chandcj" min-width="120" />
          <el-table-column label="样品名称" align="center" prop="yangpmc" min-width="120" />
          <el-table-column label="检测结论" align="center" prop="jiancjl" min-width="120" />
          <el-table-column label="检测结果" align="center" prop="reportResult" min-width="120">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.report_result" :value="scope.row.reportResult" />
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                v-hasPermi="['ww:bsWwsy:edit']">修改</el-button>
              <el-button size="mini" type="text" @click="onUploadFile(scope.row)" v-hasPermi="['ww:bsWwsy:edit']"><span
                  class="iconfont icon-shangchuanfujian"></span>附件</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                v-hasPermi="['ww:bsWwsy:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize" @pagination="getList" />
      </el-col>
    </el-row>
    <!-- 添加或修改外委试验信息主对话框 -->
    <jy-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="报告编号" prop="baogbh">
              <el-input v-model="form.baogbh" placeholder="请输入报告编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报告日期" prop="baogrq">
              <el-date-picker clearable v-model="form.baogrq" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择报告日期" style="width: 100%;">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="检测单位" prop="jiancdw">
              <el-input v-model="form.jiancdw" placeholder="请输入检测单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检测内容" prop="jiancnr">
              <el-input v-model="form.jiancnr" placeholder="请输入检测内容" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="材料类型" prop="jctzVo.clType">
              <el-select v-model="form.jctzVo.clType" placeholder="请选择材料类型" style="width: 100%;">
                <el-option v-for="dict in dict.type.cl_type" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="规格型号" prop="guigxh">
              <el-input v-model="form.jctzVo.guigxh" placeholder="请输入规格型号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="进场数量" prop="inNum">
              <el-input type="number" v-model="form.jctzVo.inNum" placeholder="请输入进场数量">
                <el-select v-model="form.jctzVo.inUnit" slot="append" placeholder="请选择" style="width: 100px;">
                  <el-option v-for="dict in dict.type.unit" :key="dict.value" :label="dict.label"
                    :value="dict.value"></el-option>
                </el-select>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="进场日期" prop="inDate">
              <el-date-picker clearable v-model="form.jctzVo.inDate" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择进场日期" style="width: 100%;"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="生产厂家">
              <el-input v-model="form.jctzVo.chandcj" placeholder="请输入生产厂家" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出厂日期">
              <el-date-picker clearable v-model="form.jctzVo.chucDate" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择出厂日期" style="width: 100%;"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="存放位置">
              <el-input v-model="form.jctzVo.cunfwz" placeholder="请输入存放位置" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="批号">
              <el-input v-model="form.jctzVo.pih" placeholder="请输入批号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="样品名称" prop="yangpmc">
              <el-input v-model="form.yangpmc" placeholder="请输入样品名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="委托日期" prop="weitrq">
              <el-date-picker clearable v-model="form.weitrq" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择委托日期" style="width: 100%;">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="登记人" prop="dengjr">
              <el-input v-model="form.dengjr" placeholder="请输入登记人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="见证人" prop="jianzr">
              <el-input v-model="form.jianzr" placeholder="请输入见证人" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="检测结论" prop="jiancjl">
          <el-input type="textarea" v-model="form.jiancjl" :rows="2" placeholder="请输入检测结论" />
        </el-form-item>
        <el-form-item label="备注" prop="beiz">
          <el-input type="textarea" v-model="form.beiz" :rows="2" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="检测结果" prop="reportResult">
          <el-radio-group v-model="form.reportResult">
            <el-radio v-for="dict in dict.type.report_result" :key="dict.value" :label="dict.value">{{ dict.label
              }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </jy-dialog>
    <file-upload-view ref="fileUploadViewRef" :moduleKey="'wwsy'" />

  </div>
</template>

<script>
import { listBsWwsy, getBsWwsy, delBsWwsy, addBsWwsy, updateBsWwsy } from "@/api/ww/bsWwsy";
import { mapGetters } from "vuex";
import { syTpyeTree } from "@/api/base/syType";
import FileUploadView from '@/components/FileUpload/FileUploadView.vue';

export default {
  name: "BsWwsy",
  dicts: ['report_result', "cl_type", "unit"],
  components: {
    FileUploadView
  },
  data () {
    return {
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 试验类型列表
      syTypeList: [],
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 外委试验信息主表格数据
      bsWwsyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        baogbh: undefined,
        syTypeId: undefined,
        deptId: undefined,
        tzId: undefined,
        baogrq: undefined,
        jiancdw: undefined,
        jiancnr: undefined,
        yangpmc: undefined,
        weitrq: undefined,
        dengjr: undefined,
        jianzr: undefined,
        jiancjl: undefined,
        beiz: undefined,
        reportResult: undefined,
      },
      // 表单参数
      form: {
        jctzVo: {}
      },
      // 表单校验
      rules: {
        baogbh: [
          { required: true, message: "报告编号不能为空", trigger: "blur" }
        ],
        baogrq: [
          { required: true, message: "报告日期不能为空", trigger: "blur" }
        ],
        jiancdw: [
          { required: true, message: "检测单位不能为空", trigger: "blur" }
        ],
        jiancnr: [
          { required: true, message: "检测内容不能为空", trigger: "blur" }
        ],
        'jctzVo.clType': [
          { required: true, message: "材料类型不能为空", trigger: "blur" }
        ],
        yangpmc: [
          { required: true, message: "样品名称不能为空", trigger: "blur" }
        ],
        weitrq: [
          { required: true, message: "委托日期不能为空", trigger: "blur" }
        ],
        reportResult: [
          { required: true, message: "检测结果不能为空", trigger: "change" }
        ],

      }
    };
  },
  // 监听顶部全局下拉
  computed: {
    ...mapGetters(["deptId"])
  },
  watch: {
    deptId (newValue, oldValue) {
      this.queryParams.deptId = newValue;
      if (this.queryParams.syTypeId) {
        this.getList();
      }
    }
  },
  created () {
    this.queryParams.deptId = this.$store.state.dept.deptId;
    this.getSyTypeTreeList();
  },
  methods: {
    /** 查询试验类型树 */
    getSyTypeTreeList () {
      let param = {
        level: 2
      };
      syTpyeTree(param).then((response) => {
        this.syTypeList = response.data;
      });
    },
    handleNodeClick (data) {
      this.form.syTypeId = undefined;
      this.queryParams.syTypeId = undefined;

      if (data.level == 2) {
        this.queryParams.syTypeId = data.id;
        this.form.syTypeId = data.id;
        this.handleQuery();
      }
    },
    /** 查询外委试验信息主列表 */
    getList () {
      this.loading = true;
      listBsWwsy(this.queryParams).then(response => {
        this.bsWwsyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        wwsyId: undefined,
        baogbh: undefined,
        syTypeId: this.queryParams.syTypeId,
        deptId: this.queryParams.deptId,
        tzId: undefined,
        baogrq: undefined,
        jiancdw: undefined,
        jiancnr: undefined,
        yangpmc: undefined,
        weitrq: undefined,
        dengjr: undefined,
        jianzr: undefined,
        jiancjl: undefined,
        beiz: undefined,
        reportResult: "1",
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        jctzVo: {
          chandcj: undefined,
          chucDate: undefined,
          clType: undefined,
          cunfwz: undefined,
          guigxh: undefined,
          inDate: undefined,
          inNum: undefined,
          inUnit: undefined,
          pih: undefined,
        }
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.wwsyId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd () {
      if (!this.queryParams.syTypeId) {
        this.$message.warning("请先选择一个合法的试验类型");
        return;
      }
      this.reset();
      this.open = true;
      this.title = "添加外委试验信息";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.loading = true;
      this.reset();
      const wwsyId = row.wwsyId || this.ids;
      getBsWwsy(wwsyId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改外委试验信息主";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.wwsyId != null) {
            updateBsWwsy(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addBsWwsy(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const wwsyIds = row.wwsyId || this.ids;
      this.$modal.confirm('是否确认删除报告编号为"' + row.baogbh + '"的数据项？').then(() => {
        this.loading = true;
        return delBsWwsy(wwsyIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('ww/bsWwsy/export', {
        ...this.queryParams
      }, `bsWwsy_${new Date().getTime()}.xlsx`);
    },
    // 附件上传
    onUploadFile (row) {
      this.$refs.fileUploadViewRef.init(row.wwsyId);
    },
  }
};
</script>
